<template>
  <div class="app-container">
    <div class="filter-container add1">
      <el-form style="">
        <el-form-item>
          <!-- 按钮级别的控制,使用hasPerm来判断是否应该有添加这个按钮 -->
          <el-button type="primary" icon="plus" @click="showCreate" v-if="hasPerm('company:add')">添加
          </el-button>

          <!-- 下载模板 -->
          <el-button @click="exportExcel" type="primary" icon="plus" v-if="hasPerm('company:add')">下载导入模板
          </el-button>

						<!-- 使用后端导入 -->
            <el-button type="primary" icon="plus" v-if="hasPerm('company:add')" size="small">导入
              <input type="file" id = "btn" ref="clearFile" name = "file" @change="importf(this)" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"/>
          </el-button>
            
          <el-button @click="exportExcelOpertor" type="primary" icon="plus" v-if="hasPerm('company:add')" >导出</el-button>
          <el-button @click="showDisplay" type="primary" icon="plus" >显示字段</el-button>
          <!-- <el-button @click="exportExcel" type="primary" icon="plus" v-if="hasPerm('article:add')" >导出</el-button> -->
           <el-button type="danger" v-if="multipleSelectionFlag" @click="delSelect">
              批量删除
          </el-button>
        </el-form-item>
      </el-form>
      
    </div>
    <div class="searchWord">
      <!-- style="display: inline-block;"  -->
        <el-input v-model="search" 
          placeholder="请输入搜索内容" @input="inputFunction()">
        </el-input>
    </div>
    <br>
    <el-table class="tabless" :data="list" v-loading.body="listLoading" element-loading-text="拼命加载中" border fit
              highlight-current-row :row-class-name="tableRowClassName" @sort-change='sortChange' @selection-change="handleSelectionChange">
              <el-table-column
      type="selection"
      width="55"
      v-if="displayAttribute.select">
    </el-table-column>
      <el-table-column align="center" label="序号" width="80" v-if="displayAttribute.id">
        <template slot-scope="scope">
          <!-- 岗位信息的索引 -->
          <span v-text="getIndex(scope.$index)"> </span>
          <!-- 岗位信息的id -->
          <!-- <span v-text="scope.row.id"> </span> -->
        </template>
      </el-table-column>
      <el-table-column align="center" prop="company_name" label="公司名称" style="width: 60px;" v-if="displayAttribute.company_name"></el-table-column>
      <el-table-column align="center" prop="company_desc" label="公司描述" style="width: 60px;" v-if="displayAttribute.company_desc"></el-table-column>
      <el-table-column align="center" prop="address" label="公司地址" style="width: 60px;" v-if="displayAttribute.address"></el-table-column>
      <el-table-column align="center" prop="telephone" label="公司电话" style="width: 60px;" v-if="displayAttribute.telephone"></el-table-column>
        <!-- <template slot-scope="scope">
          <span>{{scope.row.createTime}}</span>
        </template> -->
      </el-table-column>

      <el-table-column align="center" label="管理" width="300" v-if="hasPerm('company:update') & displayAttribute.manageMent">
        <template slot-scope="scope">
          <el-button type="primary" icon="edit" @click="showDetails(scope.$index)">详情</el-button>
          <el-button type="primary" icon="edit" @click="showUpdate(scope.$index)">修改</el-button>
          <el-button type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow" >确 定</el-button>
            </span>
        </el-dialog>

        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisibleByBatch" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定批量删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisibleByBatch = false">取 消</el-button>
                <el-button type="primary" @click="delAll" >确 定</el-button>
            </span>
        </el-dialog>
    
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum" 
      :page-size="listQuery.pageRow"
      :total="totalCount"
      :page-sizes="[3, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      class="pagination">
    </el-pagination>

    
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form class="small-space" :model="tempCompany" label-company="left" label-width="150px"
               style='width:600px; margin-left:60px;'>
            
          <el-form-item label="公司名称" v-if="dialogStatus=='create' | dialogStatus=='update' | dialogStatus=='detail'" :required="true">
            <el-input type="text" :maxlength="20"  :minlength="2" placeholder="字数应在2~20字，可以输入汉字及字母，不允许输入数字" v-model="tempCompany.company_name" @keyup.native="handlerCompanyName" style='width:400px;'>
            </el-input>
            <el-tag type="danger" v-if="this.displayInfoFromBack">{{infoFromBack}}</el-tag>
            <el-tag type="danger" v-if="this.company_name_flag">{{company_name_infor}}</el-tag>
          </el-form-item>

          <el-form-item label="公司电话" v-if="dialogStatus=='create' | dialogStatus=='update' | dialogStatus=='detail'"  :required="true">
            <el-input type="text" :maxlength="12" placeholder="只能输入3位区号加7-8位号码，中间用'-'分隔" v-model="tempCompany.telephone" @keyup.native="handlerTelephone" style='width:400px;'>
            </el-input>
            <el-tag type="danger" v-if="this.telephone_flag">{{telephone_infor}}</el-tag>
          </el-form-item>

          <el-form-item label="公司地址" v-if="dialogStatus=='create' | dialogStatus=='update' | dialogStatus=='detail'" :required="true">
            <el-input type="text" :maxlength="50" placeholder="字数应在50字以内，且只能包含汉字、字母及数字" v-model="tempCompany.address" @keyup.native="handlerAddress" style='width:400px;'>
            </el-input>
            <el-tag type="danger" v-if="this.address_flag">{{address_infor}}</el-tag>
          </el-form-item>

          <el-form-item label="工商注册号" v-if="dialogStatus=='create' | dialogStatus=='update' | dialogStatus=='detail'" :required="true">
            <el-input type="text" :maxlength="21" placeholder="只能包含13位大写字母及数字" v-model="tempCompany.register_id" @keyup.native="handlerRegisterId" style='width:400px;'>
            </el-input>
            <el-tag type="danger" v-if="this.register_id_flag">{{register_id_infor}}</el-tag>
          </el-form-item>

          <el-form-item label="纳税人识别号" v-if="dialogStatus=='create' | dialogStatus=='update' | dialogStatus=='detail'" :required="true">
          <el-input type="text" :maxlength="20" placeholder="只能包含15或17或18或20位字母和数字" v-model="tempCompany.taxpayer_identification_number" @keyup.native="handlerTaxpayer" style='width:400px;'>
          </el-input>
          <el-tag type="danger" v-if="this.taxpayer_identification_number_flag">{{taxpayer_identification_number_infor}}</el-tag>
        </el-form-item>

        <el-form-item label="组织机构代码" v-if="dialogStatus=='create' | dialogStatus=='update' | dialogStatus=='detail'" :required="true">
          <el-input type="text" :maxlength="8" placeholder="只能包含8位字母和数字"  v-model="tempCompany.organizational_code" @keyup.native="handlerOrganizational" style='width:400px;'>
          </el-input>
          <el-tag type="danger" v-if="this.organizational_code_flag">{{organizational_code_infor}}</el-tag>
        </el-form-item>

        <el-form-item label="统一社会信用代码" v-if="dialogStatus=='create' | dialogStatus=='update' | dialogStatus=='detail'" :required="true">
          <el-input type="text" :maxlength="13" placeholder="只能包含13位数字"  v-model="tempCompany.social_credit_code" @keyup.native="handlerSocial" style='width:400px;'>
          </el-input>
          <el-tag type="danger" v-if="this.social_flag">{{social_infor}}</el-tag>
        </el-form-item>

        <el-form-item label="公司邮箱" v-if="dialogStatus=='create' | dialogStatus=='update' | dialogStatus=='detail'">
          <el-input type="text" v-model="tempCompany.email" @keyup.native="handlerEmail" style='width:400px;'>
          </el-input>
          <el-tag type="danger" v-if="this.mail_flag">{{mail_infor}}</el-tag>
        </el-form-item>

        <el-form-item label="公司传真" v-if="dialogStatus=='create' | dialogStatus=='update' | dialogStatus=='detail'">
          <el-input type="text" :maxlength="13" v-model="tempCompany.fax" @keyup.native="handlerFax" style='width:400px;'>
          </el-input>
          <el-tag type="danger" v-if="this.fax_flag">{{fax_infor}}</el-tag>
        </el-form-item>
        
        <el-form-item label="公司描述" v-if="dialogStatus=='create' | dialogStatus=='update' | dialogStatus=='detail'">
          <el-input type="textarea" :rows="5" :maxlength="100" placeholder="字数应在100字以内" @keyup.native="handlerDesc" v-model="tempCompany.company_desc" style='width:400px;' ref='company_desc'>
          </el-input>
        </el-form-item>


        <div v-if="dialogStatus=='dispalyAtt'">
          <el-checkbox v-model="displayAttribute.select" disabled>复选框</el-checkbox><br>
          <el-checkbox v-model="displayAttribute.id" disabled>序号</el-checkbox><br>
          <el-checkbox v-model="displayAttribute.company_name" disabled>公司名称</el-checkbox><br>
          <el-checkbox v-model="displayAttribute.company_desc">公司描述</el-checkbox><br>
          <el-checkbox v-model="displayAttribute.address">公司地址</el-checkbox><br>
          <el-checkbox v-model="displayAttribute.telephone">公司电话</el-checkbox><br>
          <el-checkbox v-model="displayAttribute.manageMent" disabled>管理</el-checkbox>

        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="success" @click="createCompany" :disabled = "enableCreate">创 建</el-button>
        <el-button type="primary" v-if="dialogStatus=='update'" @click="updateCompany" :disabled = "enableUpdate">修 改</el-button>
        
      </div>
    </el-dialog>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible2">
      <el-form class="small-space" :model="tempCompany" label-company="left" label-width="150px"
               style='width:600px; margin-left:60px;'>
        <el-form-item label="公司名称：">
          <i class="el-icon-pri">{{tempCompany.company_name}}</i> 
        </el-form-item>
        <el-form-item label="公司描述：">
          <i class="el-icon-pri">{{tempCompany.company_desc}}</i>
        </el-form-item>
        <el-form-item label="公司电话：">
          <i class="el-icon-pri">{{tempCompany.telephone}}</i> 
        </el-form-item>
        <el-form-item label="公司地址：">
          <i class="el-icon-pri">{{tempCompany.address}}</i> 
        </el-form-item>
        <el-form-item label="公司邮箱：">
          <i class="el-icon-pri">{{tempCompany.email}}</i> 
        </el-form-item>
        <el-form-item label="公司传真：">
          <i class="el-icon-pri">{{tempCompany.fax}}</i>          
        </el-form-item>
        <el-form-item label="工商注册号：">
          <i class="el-icon-pri">{{tempCompany.register_id}}</i>          
        </el-form-item>
        <el-form-item label="统一社会信用代码：">
          <i class="el-icon-pri">{{tempCompany.social_credit_code}}</i>          
        </el-form-item>
        <el-form-item label="纳税人识别号：">
          <i class="el-icon-pri">{{tempCompany.taxpayer_identification_number}}</i>          
        </el-form-item>
        <el-form-item label="组织机构代码：">
          <i class="el-icon-pri">{{tempCompany.organizational_code}}</i>          
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>

  import FileSaver from "file-saver";
  import XLSX from "xlsx";
  import {mapGetters} from 'vuex';
  export default {
    data() {
      return {
        readonly: false,
        totalCount: 0, //分页组件--数据总条数
        list: [],//表格的数据
        listLoading: false,//数据加载等待动画
        listQuery: {
          pageNum: 1,//页码
          pageRow: 5,//每页条数
          name: ''
        },
        // 为textMap中的属性,指定标题头
        dialogStatus: 'create',
        dialogFormVisible: false,
        dialogFormVisible2: false,
        textMap: {
          update: '修改公司信息',
          create: '创建公司信息',
          detail: '公司详细信息',
          dispalyAtt: '显示字段'
        },
        tempCompany: {
          id: "",
          company_name: "",
          company_desc: "",
          telephone: "",
          email: "",
          fax: "",
          address: "",
          register_id: "",
          social_credit_code: "",
          taxpayer_identification_number: "",
          organizational_code: ""
        },
        delVisible:false,//删除提示弹框的状态
        delVisibleByBatch:false,//批量删除提示弹框的状态
        msg:"",//记录每一条的信息，便于取id
        delarr:[],//存放删除的数据
        multipleSelection:[],//多选的数据
        // 搜索框中的内容
        search:'',
        // 排序的列名
        prop:'',
        // 排序的方式:包括asc,desc等
        order:'',
        // 记录导出得时候跳页的之前的页数
        temp_page:3,

        // 从后台返回来的数据
        infoFromBack:'',
        displayInfoFromBack:false,

        // 验证表单数据
        company_name_infor : '',
        company_name_flag : false,
        telephone_infor : '',
        telephone_flag : false,
        address_infor : '',
        address_flag : false,
        register_id_infor : '',
        register_id_flag : false,
        taxpayer_identification_number_infor : '',
        taxpayer_identification_number_flag : false,
        organizational_code_infor : '',
        organizational_code_flag : false,
        mail_infor : '',
        mail_flag : false,
        fax_infor : '',
        fax_flag : false,
        social_flag : false,
        social_infor : '',

        enableCreate:false,

        enableUpdate:false,

        multipleSelectionFlag : true,
        // 记录显示字段
        displayAttribute:{
          select:true,
          id:true,
          company_name: true,
          company_desc: true,
          telephone: true,
          address: true,
          manageMent:true
        }
      }
    },
    computed:{
      // 模糊搜索
      // tables () {
      //   const search = this.search
      //   console.log('search')
      //   if (search) {
      //     // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
      //     // 注意： filter() 不会对空数组进行检测。
      //     // 注意： filter() 不会改变原始数组。
      //     return this.list.filter(data => {
      //       // some() 方法用于检测数组中的元素是否满足指定条件;
      //       // some() 方法会依次执行数组的每个元素：
      //       // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
      //       // 如果没有满足条件的元素，则返回false。
      //       // 注意： some() 不会对空数组进行检测。
      //       // 注意： some() 不会改变原始数组。
      //       return Object.keys(data).some(key => {
      //         // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
      //         // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
      //         return String(data[key]).toLowerCase().indexOf(search) > -1
      //       })
      //     })
      //   }
      //   return this.list
      // }
      // tables () {
      //   const search = this.search;
      //   // console.log("search:");
      //   // console.log(search);
      //   if (search) {
      //     // console.log("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
      //     
      //   }
      //   return this.list
      // }
    },
    created() {
      this.getList();
      // this.showUserInfo();
    },
    computed: {
      ...mapGetters([
        'userId',
        'nickname'
      ])
    },
    methods: {

      handlerCompanyName(){
        //判断公司名称
        var chineseCharacters = /^[\u0391-\uFFE5A-Za-z]+$/
        var chinese = new RegExp(chineseCharacters)
        if(chinese.test(this.tempCompany.company_name) && this.tempCompany.company_name.length < 2){
          this.company_name_flag = true;
          this.company_name_infor = "输入字数必须多于2个";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else if(!chinese.test(this.tempCompany.company_name)){
          this.company_name_flag = true;
          this.company_name_infor = "必须输入汉字或英文";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else if(this.tempCompany.company_name.length === 20){
          this.company_name_flag = true;
          this.company_name_infor = "输入字数必须少于20个";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else{
          this.company_name_flag = false;
          this.company_name_infor = "";
          
          this.api({
          url: "/company/getCompanyByContent",
          method: "get",
          params: this.tempCompany
        }).then(data => {
          if(data === "fail"){
            // alert("无效");
            this.displayInfoFromBack = true;
            this.infoFromBack = "无效";
            this.enableCreate = true;
            this.enableUpdate = true;
          }
          else{
            this.displayInfoFromBack = false;
            this.infoFromBack = "";
            this.handlerCompany()
          }
        })
        }
        //
      },

      handlerTelephone(){
        //判断公司电话
        var phoneNumber = /^0[0-9]{2,3}-[0-9]{7,8}$/
        var phone = new RegExp(phoneNumber)

        if(!phone.test(this.tempCompany.telephone)){
          this.telephone_flag = true;
          this.telephone_infor = "电话号码格式不正确，应输入如010-1234567或010-12345678";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else if(this.tempCompany.telephone.length === 0){
          this.telephone_flag = true;
          this.telephone_infor = "请输入电话号码";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else{
          this.telephone_flag = false;
          this.telephone_infor = "";
          
          this.api({
          url: "/company/getTelephoneByContent",
          method: "get",
          params: this.tempCompany
        }).then(data => {
          if(data === "fail"){
            // alert("无效");
            this.telephone_flag = true;
            this.telephone_infor = "无效";
            this.enableCreate = true;
            this.enableUpdate = true;
          }
          else{
            this.telephone_flag = false;
            this.telephone_infor = "";
            this.handlerCompany()
          }
        })
        }
      },

      handlerAddress(){
        //判断公司地址
        var addressCharacters = /^[\u4e00-\u9fa5a-zA-Z0-9]+$/
        var address = new RegExp(addressCharacters)
        if(this.tempCompany.address.length === 0){
          this.address_flag = true;
          this.address_infor = "请输入地址";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else if(this.tempCompany.address.length === 50){
          this.address_flag = true;
          this.address_infor = "输入字数必须少于50字";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else if(!address.test(this.tempCompany.address)){
          this.address_flag = true;
          this.address_infor = "地址只能包含汉字、字母及数字";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else{
          this.address_flag = false;
          this.address_infor = "";
          
          this.api({
          url: "/company/getAddressByContent",
          method: "get",
          params: this.tempCompany
        }).then(data => {
          if(data === "fail"){
            // alert("无效");
            this.address_flag = true;
            this.address_infor = "无效";
            this.enableCreate = true;
            this.enableUpdate = true;
          }
          else{
            this.address_flag = false;
            this.address_infor = "";
            this.handlerCompany()
          }
        })
        }
      },

      handlerRegisterId(){
        //判断公司工商注册号
        // var registerCharacters = /^$/
        var registerCharacters = /^[1-9A-GY]{1}[1239]{1}[1-5]{1}[0-9]{5}[0-9A-Z]{10}$|[1-9A-GY]{1}[1239]{1}[1-5]{1}[0-9]{5}[0-9A-Z]{10}-[0-9]{2}$/
        var register = new RegExp(registerCharacters)
        if(this.tempCompany.register_id.length === 0){
          this.register_id_flag = false;
          // this.register_id_infor = "请输入工商注册号";
          this.enableCreate = false;
          this.enableUpdate = false;
        }
        else if(!register.test(this.tempCompany.register_id)){
          this.register_id_flag = false;
          this.register_id_infor = "只能包含13位大写字母及数字";
          this.enableCreate = false;
          this.enableUpdate = false;
        }
        else{
          this.register_id_flag = false;
          this.register_id_infor = "";
          
          this.api({
          url: "/company/getRegisterIdByContent",
          method: "get",
          params: this.tempCompany
        }).then(data => {
          if(data === "fail"){
            // alert("无效");
            this.register_id_flag = true;
            this.register_id_infor = "无效";
            this.enableCreate = true;
            this.enableUpdate = true;
          }
          else{
            this.register_id_flag = false;
            this.register_id_infor = "";
            this.handlerCompany()
          }
        })
        }
      },

      handlerTaxpayer(){
        //判断纳税人识别号
        var taxpayerCharacters = /^[A-Z0-9]{15}$|^[A-Z0-9]{17}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/
        var taxpayer = new RegExp(taxpayerCharacters)
        if(this.tempCompany.taxpayer_identification_number.length === 0){
          this.taxpayer_identification_number_flag = true;
          this.taxpayer_identification_number_infor = "请输入纳税人识别号";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else if(!taxpayer.test(this.tempCompany.taxpayer_identification_number)){
          this.taxpayer_identification_number_flag = true;
          this.taxpayer_identification_number_infor = "只能由15或17或18或20位字母、数字组成";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else{
          this.taxpayer_identification_number_flag = false;
          this.taxpayer_identification_number_infor = "";
          
          this.api({
          url: "/company/getTaxpayerByContent",
          method: "get",
          params: this.tempCompany
        }).then(data => {
          if(data === "fail"){
            // alert("无效");
            this.taxpayer_identification_number_flag = true;
            this.taxpayer_identification_number_infor = "无效";
            this.enableCreate = true;
            this.enableUpdate = true;
          }
          else{
            this.taxpayer_identification_number_flag = false;
            this.taxpayer_identification_number_infor = "";
            this.handlerCompany()
          }
        })
        }
      },

      handlerOrganizational(){
        //判断公司组织机构代码
        var organizationalCharacters = /^[A-Z0-9]{8}-[A-Z0-9]$|[A-Z0-9]{8}-[A-Z0-9]-[0-9]{2}$/
        var organizational = new RegExp(organizationalCharacters)
        if(this.tempCompany.organizational_code.length === 0){
          this.organizational_code_flag = false;
          this.organizational_code_infor = "请输入组织机构代码";
          this.enableCreate = false;
          this.enableUpdate = false;
        }
        else if(!organizational.test(this.tempCompany.organizational_code)){
          this.organizational_code_flag = false;
          this.organizational_code_infor = "只能包含8位字母和数字";
          this.enableCreate = false;
          this.enableUpdate = false;
        }
        else{
          this.organizational_code_flag = false;
          this.organizational_code_infor = "";
          
          this.api({
          url: "/company/getOrganizationalByContent",
          method: "get",
          params: this.tempCompany
        }).then(data => {
          if(data === "fail"){
            // alert("无效");
            this.organizational_code_flag = true;
            this.organizational_code_infor = "无效";
            this.enableCreate = true;
            this.enableUpdate = true;
          }
          else{
            this.organizational_code_flag = false;
            this.organizational_code_infor = "";
            this.handlerCompany()
          }
        })
        }
      },

      handlerSocial(){
        //判断公司统一社会信用代码
        var socialCharacters = /^[0-9]{13}$|^[0-9]{13}-[0-9]{2}$/
        var social = new RegExp(socialCharacters)
        if(this.tempCompany.social_credit_code.length === 0){
          this.social_flag = true;
          this.social_infor = "请输入统一社会信用代码";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else if(!social.test(this.tempCompany.social_credit_code)){
          this.social_flag = true;
          this.social_infor = "只能包含13位数字";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else{
          this.social_flag = false;
          this.social_infor = "";
          
          this.api({
          url: "/company/getSocialByContent",
          method: "get",
          params: this.tempCompany
        }).then(data => {
          if(data === "fail"){
            // alert("无效");
            this.social_flag = true;
            this.social_infor = "无效";
            this.enableCreate = true;
            this.enableUpdate = true;
          }
          else{
            this.social_flag = false;
            this.social_infor = "";
            this.handlerCompany()
          }
        })
        }
      },

      handlerEmail(){
        //判断邮箱格式
        var mailCharacters = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/
                              //^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$
        var mail = new RegExp(mailCharacters)

        if(!mail.test(this.tempCompany.email)){
          this.mail_flag = true;
          this.mail_infor = "邮箱格式不正确";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else{
          this.mail_flag = false;
          this.mail_infor = "";
          
          this.api({
          url: "/company/getMailByContent",
          method: "get",
          params: this.tempCompany
        }).then(data => {
          if(data === "fail"){
            // alert("无效");
            this.mail_flag = true;
            this.mail_infor = "无效";
            this.enableCreate = true;
            this.enableUpdate = true;
          }
          else{
            this.mail_flag = false;
            this.mail_infor = "";
            this.handlerCompany()
          }
        })
        }
      },

      handlerFax(){
        //判断传真
        var faxCharacters =  /^((0\d{2,3}-)?\d{7,8})$/
        var fax = new RegExp(faxCharacters)
        if(!fax.test(this.tempCompany.fax)){
          this.fax_flag = true;
          this.fax_infor = "传真格式不正确";
          this.enableCreate = true;
          this.enableUpdate = true;
        }
        else{
          this.fax_flag = false;
          this.fax_infor = "";
          
          this.api({
          url: "/company/getFaxByContent",
          method: "get",
          params: this.tempCompany
        }).then(data => {
          if(data === "fail"){
            // alert("无效");
            this.fax_flag = true;
            this.fax_infor = "无效";
            this.enableCreate = true;
            this.enableUpdate = true;
          }
          else{
            this.fax_flag = false;
            this.fax_infor = "";
            this.handlerCompany()
          }
        })
        }
      },

      handlerCompany(){
        // return;
        if(!this.fax_flag && !this.mail_flag && !this.social_flag && !this.organizational_code_flag && !this.taxpayer_identification_number_flag && !this.register_id_flag && !this.address_flag && !this.telephone_flag && !this.company_name_flag && !this.displayInfoFromBack){
            if(this.tempCompany.social_credit_code.length != 0 && this.tempCompany.organizational_code.length != 0 && this.tempCompany.taxpayer_identification_number.length != 0 && this.tempCompany.register_id.length != 0 && this.tempCompany.address.length != 0 && this.tempCompany.telephone.length != 0 && this.tempCompany.company_name.length != 0){
                this.enableCreate = false;
                this.enableUpdate = false;
            }
        } 
      },

      clearFile() {
        this.$refs.clearFile.value = ''; // 清空file文件
      },
      // 导入
      importf(obj) {
        let _this = this;
        let inputDOM = this.$refs.inputer;   // 通过DOM取文件数据
        this.file = event.currentTarget.files[0];  
        var rABS = false; //是否将文件读取为二进制字符串
        var f = this.file;
        var reader = new FileReader();
        //if (!FileReader.prototype.readAsBinaryString) {
        FileReader.prototype.readAsBinaryString = function(f) {
        var binary = "";
        var rABS = false; //是否将文件读取为二进制字符串
        var pt = this;
        var wb; //读取完成的数据
        var outdata;
        var reader = new FileReader();
        reader.onload = function(e) {
              var bytes = new Uint8Array(reader.result);
              var length = bytes.byteLength;
              for(var i = 0; i < length; i++) {
                  binary += String.fromCharCode(bytes[i]);
              }
              var XLSX = require('xlsx');
              if(rABS) {
                  wb = XLSX.read(btoa(fixdata(binary)), { //手动转化
                        type: 'base64'
                  });
              } else {
                  wb = XLSX.read(binary, {
                        type: 'binary'
                  });
              }
              // outdata就是你想要的东西 excel导入的数据
              outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]); 
              // excel 数据再处理
              let arr = []
              outdata.map(v => {
                  // console.log(v)
                  let obj = {}
                  obj.company_name = v.公司名称
                  obj.company_desc = v.公司描述
                  obj.telephone = v.公司电话
                  obj.address = v.公司地址
                  obj.email = v.公司邮箱
                  obj.fax = v.公司传真
                  obj.register_id = v.工商注册号
                  obj.social_credit_code = v.统一社会信用代码
                  obj.id = ""
                  obj.taxpayer_identification_number = v.纳税人识别号
                  obj.organizational_code = v.组织机构代码
                  arr.push(obj)
                  _this.tempCompany = obj
                  _this.createCompany()
                  // this.api({
                  //   url: "/article/addArticle",
                  //   method: "post",
                  //   data: obj
                  // }).then(() => {
                  //   this.getList();
                  //   // this.dialogFormVisible = false
                  // })
              })
              _this.accountList = [...arr];
              // _this.reload();
              // console.log('uuuuuuuuuuuuuuuuuuuuu');
            }
            alert("上传完毕！")
            _this.clearFile()
            reader.readAsArrayBuffer(f);
        }
        if(rABS) {
              reader.readAsArrayBuffer(f);
        } else {
              reader.readAsBinaryString(f);
        }
    },

      // 导入结束
    exportExcelOpertor(){
      // let tempPageSize = this.listQuery.pageRow;
      // alert(tempPageSize);
      // this.exportExcel();
      this.temp_page = this.listQuery.pageRow;
      // this.listQuery.pageRow = this.totalCount;
      this.handleSizeChange(this.totalCount)
      // this.listQuery.pageRow = this.totalCount;
      // alert("1")
      // this.exportExcel();
      
      
      // alert("2")
      // this.exportExcel();
      window.setTimeout(this.exportExcel,50);
      // alert("3")
      // window.setTimeout("")
      window.setTimeout(this.delayTime,200);
    },
    delayTime(){
      this.listQuery.pageRow = this.temp_page
      this.generalFun();
    },

    exportExcel () {
      // alert("exportExcel")
      // 设置当前日期
      let time = new Date();
      let year = time.getFullYear();
      let month = time.getMonth() + 1;
      let day = time.getDate();
      let name = year + "" + month + "" + day;
      // alert(this.totalCount);
      
      // alert(this.listQuery.pageRow);
      // this.listQuery.pageNum = 1;
      // this.getList();
      // alert("zandaoguang");
      // return
      this.$nextTick(function () {
          // alert("iiiiiiiiiiiiiiiii");
          // alert(tempPageSize);
          let wb = XLSX.utils.table_to_book(document.querySelector('.tabless'));
          /* get binary string as output */
          let wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: true, type: 'array'});
          try {
              FileSaver.saveAs(new Blob([wbout], {type: 'application/octet-stream'}), name + ".xlsx")
          } catch (e) {
              if (typeof console !== 'undefined') console.log(e, wbout)
          }
          // this.listQuery.pageRow = 3;//表格还原
          // this.getList();
          return wbout
      }
      )
  },
                
                
            inputFunction(){
              // console.log(this.search);
              // console.log(this.search == "");
              
              if(this.search){
                // console.log('aaaaaaaaaaaaaaaa');
                this.getListBySearch(this.search);
              }
              if(this.search == ""){
                this.getList();
              }
            },
            sortChange(column, prop, order) {

              // this.handleCurrentChange(1);
              this.listQuery.pageNum = 1;
              // 点击排序按钮后拿到column.order，可以发送column.order给后台，后台再根据什么排序来返回排序过后的数据
              // console.log(column);
              // console.log(column + '---' + column.prop + '---' + column.order)
              // this.prop = column.prop;
              // this.order = column.order;
              // console.log(this.prop);
              // console.log(column.prop);
              // console.log(this.order);
              // 如果搜索框中没有搜索内容
              if(this.search == ""){
                  if(column.order === "descending"){
                    this.listQuery.sortby = column.prop;
                    this.listQuery.order = 'DESC';
                    // this.prop = column.prop;
                    // this.order = 'DESC';
                  }else{
                    this.listQuery.sortby = column.prop;
                    this.listQuery.order = 'ASC';
                    // this.prop = column.prop;
                    // this.order = 'ASC';
                  }
                  if(column.prop == "content"){
                      this.getListByOrderContent();
                      // this.prop = '';
                      // this.order = '';
                  }
                  else if(column.prop == "createTime") {
                    this.getListByOrderCreateTime();
                    // this.prop = '';
                    // this.order = '';
                  }
                  else{
                    // 默认返回id排序
                    this.getList();
                  }
              }
              // 如果搜索框中有搜索内容
              else{
                  if(column.order === "descending"){
                    this.listQuery.sortby = column.prop;
                    this.listQuery.order = 'DESC';
                    // this.prop = column.prop;
                    // this.order = 'DESC';
                  }else{
                    this.listQuery.sortby = column.prop;
                    this.listQuery.order = 'ASC';
                    // this.prop = column.prop;
                    // this.order = 'ASC';
                  }
                  if(column.prop == "content"){
                    this.getListByContentHaveSearch();
                    // this.prop = '';
                    // this.order = '';
                  }
                  else if(column.prop == "createTime")  {
                    // 当空格的时候，应该怎么排序
                    // this.getListByOrderCreateTime();
                    // 默认按照id排序
                    // this.getListBySearch(this.search);
                    this.getListByOrderCreateTimeHaveSearch();
                    // this.prop = '';
                    // this.order = '';
                  }else{
                    // 默认什么都不做
                    // 默认返回id排序
                    this.getListBySearch(this.search);
                  }
              }
              //输出的结果 [object Object]---name---ascending
            },
            handleDelete(index, row) {
                this.idx = index;
                this.msg = row;       //每一条数据的记录
                this.delarr.push(this.msg.PackingId); //把单行删除的每条数据的id添加进放删除数据的数组
                this.delVisible = true;
            },
            // 当批量删除的时候,如果要是没有选中,弹出提醒
            delWarnInfo() {
              this.$message('请选择您要删除的数据');
            },
            deleteRow(){
                let _vue = this;
                // 写自己的删除的对象
                // console.log(this.delarr)
                let company = _vue.list[_vue.idx];
                _vue.api({
                  url: "/company/deleteCompany",
                  method: "post",
                  data: company
                }).then(() => {
                  if(this.search == ""){
                    _vue.getList();
                  }
                  else{
                    // alert("我怎么就空了呢？")
                    _vue.getListBySearch(this.search);
                  }
                }).catch(() => {
                  _vue.$message.error("删除失败")
                })
                this.delVisible = false;//关闭删除提示模态框
            },
            //批量删除
            delAll() {
                // alert("我进来啦~");
                var lengths = this.multipleSelection.length;
                
                for (let i = 0; i < lengths; i++) {
                  this.api({
                  url: "/company/deleteCompany",
                  method: "post",
                  data: this.multipleSelection[i]
                }).then(() => {
                  if(this.search == ""){
                    this.getList();
                  }
                  else{
                    this.getListBySearch(this.search);
                  }
                }).catch(() => {
                  this.$message.error("删除失败")
                })
                
                }
                this.delVisibleByBatch = false;//关闭删除提示模态框
            },
            
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      delSelect(){
        const length = this.multipleSelection.length;
        if(length == 0){
          this.delWarnInfo();
        }else{
          this.delVisibleByBatch = true;//显示删除弹框
        }
        // this.delAll();
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      tableRowClassName({row, rowIndex}) {
        if (rowIndex % 2 === 1) {
          return 'warning-row';
        } else if (rowIndex % 2 === 0) {
          // return 'success-row';
        }
        return '';
      },

      getList() {
        //查询所有的岗位信息
        if (!this.hasPerm('company:list')) {
          return
        }
        this.listLoading = true;
        this.api({
          url: "/company/listCompany",
          method: "get",
          params: this.listQuery
        }).then(data => {
          // console.log(data)
          this.listLoading = false;
          this.list = data.list;
          this.tables = data.list;
          this.totalCount = data.totalCount;
        })
      },
      getListByOrderContent(){
        //升序或降序查询岗位信息

        if (!this.hasPerm('article:list')) {
          return
        }
        this.listLoading = true;
        this.api({
          url: "/article/listArticleByOrder",
          method: "get",
          params: this.listQuery
        }).then(data => {
          // console.log('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
          // console.log(data)
          this.listLoading = false;
          this.list = data.list;
          this.tables = data.list;
          this.totalCount = data.totalCount;
        })
      },

      // 当搜索框中有内容的时候，排序的时候需要只排序搜索出来的内容
      getListByContentHaveSearch(){
        //查询列表

        if (!this.hasPerm('article:list')) {
          return
        }
        this.listLoading = true;
        this.listQuery.value = this.search;
        this.api({
          url: "/article/listArticleByContentHaveSearch",
          method: "get",
          params: this.listQuery
        }).then(data => {
          console.log(data)
          this.listLoading = false;
          this.list = data.list;
          this.tables = data.list;
          this.totalCount = data.totalCount;
        })
      },

      getListByOrderCreateTimeHaveSearch(){
        //查询列表

        if (!this.hasPerm('article:list')) {
          return
        }
        this.listLoading = true;
        this.listQuery.value = this.search;
        this.api({
          url: "/article/getListByOrderCreateTimeHaveSearch",
          method: "get",
          params: this.listQuery
        }).then(data => {
          console.log(data)
          this.listLoading = false;
          this.list = data.list;
          this.tables = data.list;
          this.totalCount = data.totalCount;
        })
      },
      getListByOrderCreateTime(){
        //查询列表

        if (!this.hasPerm('article:list')) {
          return
        }
        this.listLoading = true;
        this.api({
          url: "/article/listArticleByOrderCreateTime",
          method: "get",
          params: this.listQuery
        }).then(data => {
          // console.log(data)
          this.listLoading = false;
          this.list = data.list;
          this.tables = data.list;
          this.totalCount = data.totalCount;
        })
      },

      getListBySearch(val) {
        // console.log("val:"+val);
        //查询列表
        if (!this.hasPerm('company:list')) {
          return
        }
        this.listLoading = true;
        this.listQuery.value = val;
        // console.log("this.listQuery" + this.listQuery);
        this.api({
          url: "/company/listCompanyBySearch",
          method: "get",
          params: this.listQuery
        }).then(data => {
          // console.log(data)
          this.listLoading = false;
          this.list = data.list;
          this.totalCount = data.totalCount;
        })
      },
      handleSizeChange(val) {
        //改变每页数量
        // alert("handleSizeChange()")
        this.listQuery.pageRow = val
        this.generalFun();
        // this.handleFilter();
      },
      handleCurrentChange(val) {
        //改变页码
        // console.log("sortby:",this.listQuery.sortby)
        // console.log("order:",this.listQuery.order)
        this.listQuery.pageNum = val
        this.generalFun();
        
      },
      getIndex($index) {
        //表格序号
        return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1
      },
      showCreate() {

        //清除显示无效字样
        this.displayInfoFromBack = false;
        this.infoFromBack = "";
        this.company_name_infor = '';
        this.company_name_flag = false;
        this.telephone_infor = '';
        this.telephone_flag = false;
        this.address_infor = '';
        this.address_flag = false;
        this.register_id_infor = '';
        this.register_id_flag = false;
        this.taxpayer_identification_number_infor = '';
        this.taxpayer_identification_number_flag = false;
        this.organizational_code_infor = '';
        this.organizational_code_flag = false;
        this.mail_infor = '';
        this.mail_flag = false;
        this.fax_infor = '';
        this.fax_flag = false;
        this.social_flag = false;
        this.social_infor = '';
        this.enableCreate = false;
        this.enableUpdate = false;

        //显示新增对话框
        this.tempCompany.id = "";
        this.tempCompany.company_name = "";
        this.tempCompany.company_desc = "";
        this.tempCompany.address = "";
        this.tempCompany.telephone = "";
        this.tempCompany.email = "";
        this.tempCompany.fax = "";
        this.tempCompany.register_id = "";
        this.tempCompany.social_credit_code = "";
        this.tempCompany.taxpayer_identification_number = "";
        this.tempCompany.organizational_code = "";
        this.dialogStatus = "create";
        this.dialogFormVisible = true
      },
      showDisplay(){
        this.dialogStatus = "dispalyAtt"
        this.dialogFormVisible = true
      },
      showDetails($index){

        this.dialogStatus = "detail"
        this.tempCompany.id = this.list[$index].company_id;
        //绑定相应的内容
        this.tempCompany.company_name = this.list[$index].company_name;
        this.tempCompany.company_desc = this.list[$index].company_desc;
        this.tempCompany.address = this.list[$index].address;
        this.tempCompany.telephone = this.list[$index].telephone;
        this.tempCompany.email = this.list[$index].email;
        this.tempCompany.fax = this.list[$index].fax;
        this.tempCompany.register_id = this.list[$index].register_id;
        this.tempCompany.social_credit_code = this.list[$index].social_credit_code;
        this.tempCompany.taxpayer_identification_number = this.list[$index].taxpayer_identification_number;
        this.tempCompany.organizational_code = this.list[$index].organizational_code;

        this.dialogFormVisible2 = true
      },
      showUpdate($index) {
        //显示修改对话框
        //清除显示无效字样
        this.displayInfoFromBack = false;
        this.infoFromBack = "";
        this.company_name_infor = '';
        this.company_name_flag = false;
        this.telephone_infor = '';
        this.telephone_flag = false;
        this.address_infor = '';
        this.address_flag = false;
        this.register_id_infor = '';
        this.register_id_flag = false;
        this.taxpayer_identification_number_infor = '';
        this.taxpayer_identification_number_flag = false;
        this.organizational_code_infor = '';
        this.organizational_code_flag = false;
        this.mail_infor = '';
        this.mail_flag = false;
        this.fax_infor = '';
        this.fax_flag = false;
        this.social_flag = false;
        this.social_infor = '';
        this.enableCreate = false;
        this.enableUpdate = false;

        //绑定相应的id
        this.tempCompany.id = this.list[$index].company_id;
        //绑定相应的内容
        this.tempCompany.company_name = this.list[$index].company_name;
        this.tempCompany.company_desc = this.list[$index].company_desc;
        this.tempCompany.address = this.list[$index].address;
        this.tempCompany.telephone = this.list[$index].telephone;
        this.tempCompany.email = this.list[$index].email;
        this.tempCompany.fax = this.list[$index].fax;
        this.tempCompany.register_id = this.list[$index].register_id;
        this.tempCompany.social_credit_code = this.list[$index].social_credit_code;
        this.tempCompany.taxpayer_identification_number = this.list[$index].taxpayer_identification_number;
        this.tempCompany.organizational_code = this.list[$index].organizational_code;
        this.dialogStatus = "update"
        this.dialogFormVisible = true
      },
      createCompany() {
        //保存新文章
        // console.log(this.tempPosition)
        // return;
        this.api({
          url: "/company/addCompany",
          method: "post",
          data: this.tempCompany
        }).then(() => {
          this.getList();
          this.dialogFormVisible = false
        })
      },
      updateCompany() {
        // console.log("tempPosition:"+this.tempPosition);
        //修改岗位信息
        this.api({
          url: "/company/updateCompany",
          method: "post",
          data: this.tempCompany
        }).then(() => {
          this.getList();
          this.dialogFormVisible = false
        })
      },

      generalFun(){
        if(this.search){
          if(this.listQuery.sortby == "" & this.listQuery.order == ""){
            this.getListBySearch(this.search);
          }
          else if(this.listQuery.sortby == "content" && this.listQuery.order == "ASC"){
            // alert("你好啊")
            // this.listQuery.sortby = this.prop;
            // this.listQuery.order = "ASC";
            this.getListByContentHaveSearch();
          }
          else if(this.listQuery.sortby == "content" && this.listQuery.order == "DESC"){
            // alert("很好啊")
            // this.listQuery.sortby = this.prop;
            // this.listQuery.order = "DESC";
            this.getListByContentHaveSearch();
          }
          else if(this.listQuery.sortby == "createTime" && this.listQuery.order == "ASC"){
            // this.listQuery.sortby = this.prop;
            // this.listQuery.order = "ASC";
            this.getListByOrderCreateTimeHaveSearch();
          }
          else if(this.listQuery.sortby == "createTime" && this.listQuery.order == "DESC"){
            // this.listQuery.sortby = this.prop;
            // this.listQuery.order = "DESC";
            this.getListByOrderCreateTimeHaveSearch();
          }
          else{
            // ...
          }
        }
        // 没有搜索内容，所以可以直接
        else{
          // alert("ssssssssssssssssss")
          // console.log(this.prop);
          // console.log(this.order);
          if(this.listQuery.sortby == "" & this.listQuery.order == ""){
            
            this.getList();
          }
          else if(this.listQuery.sortby == "content" && this.listQuery.order == "ASC"){
            // this.listQuery.sortby = this.prop;
            // this.listQuery.order = "ASC";
            // ...
            this.getListByOrderContent();
          }
          else if(this.listQuery.sortby == "content" && this.listQuery.order == "DESC"){
            // this.listQuery.sortby = this.prop;
            // this.listQuery.order = "DESC";
            this.getListByOrderContent();
          }
          else if(this.listQuery.sortby == "createTime" && this.listQuery.order == "ASC"){
            // this.listQuery.sortby = this.prop;
            // this.listQuery.order = "ASC";
            this.getListByOrderCreateTime();
          }
          else if(this.listQuery.sortby == "createTime" && this.listQuery.order == "DESC"){
            // this.listQuery.sortby = this.prop;
            // this.listQuery.order = "DESC";
            this.getListByOrderCreateTime();
          }else{
            // ...
            this.getList();
            // alert("我是空的！")
          }
        }
      }
    }

  }
</script>


<style>
.el-table .warning-row {
    background:whitesmoke;
    
     /* background: #ffffff; */
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
  
  .pagination{
    background-color:lavender;
    margin-right: 0%;
    text-align: right;
  }

  .searchWord .add1{
    display: inline;
  }

  #btn{
            /* padding: 5px 10px;
            background: #00b0f0;
            color: #FFF;
            border: none;
            border-radius: 5px; */
        }


</style>